#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "library.h"

List* establish_head(int size)
{
    List* new = (List*)malloc(sizeof(*new));
    if(NULL == new)
    {
        perror("malloc new fail");
        return NULL;
    }
    new -> size = size;
    new -> tou.next = &new -> tou;
    new -> tou.prev = &new -> tou;
//  new -> tou.data;
    return new;
}

//创建新节点
void establish_node(List* head, void* data, int mode)
{
    struct Node_wgc* new_node = malloc(sizeof(*new_node) + head -> size);
    if(NULL == new_node)
    {
        perror("malloc new_node fail");
        return ;
    }    
    memcpy(new_node -> data, data, head -> size);
    if(mode == _MODE_FRONT)
    {
        new_node -> next = head -> tou.next;
        new_node -> prev = &(head -> tou);
    }
    else if(mode == _MODE_AFTER)
    {
        new_node -> prev = head -> tou.prev;
        new_node -> next = &(head -> tou);
    }
    else
    {
        printf("参数错误！\n");
        return;
    }
    new_node -> next -> prev = new_node -> prev -> next = new_node; 
    return;
}

//遍历所有节点，如何处理节点自行传入所需函数
void ergodic(List* head, void (*operation)(void*))
{
    struct Node_wgc* ucr;
    for(ucr= head -> tou.next; ucr != &(head -> tou); ucr = ucr -> next)
    {
        operation(ucr -> data);
    }
}

//遍历操作所有节点，如何处理节点自行传入所需函数
void ergodic_(List* head, void (*operation)(void*, void*), void* name)
{
    struct Node_wgc* ucr;
    for(ucr= head -> tou.next; ucr != &(head -> tou); ucr = ucr -> next)
    {
        operation(ucr -> data, name);
    }
}

//判空
int null_wgc(List* head)
{
	if(head -> tou.next == &(head -> tou))
	return 1;
	return 0;
}

//静态查找1
static struct Node_wgc* find(List* head, void* key, int (*cmp)(void* key, void*))
{
    struct Node_wgc* ucr;
    for(ucr = head -> tou.next; ucr != &(head -> tou); ucr = ucr -> next)
    {
        if(cmp(key, ucr -> data) == 0)
        {
            break;
        }
    }
        return ucr;
}

//查找数据1
void* find_wgc(List* head, void* key, int (*cmp)(void* key, void*)) 
{
    struct Node_wgc* ucr;
    ucr = find(head, key, cmp);
    if(ucr != &(head -> tou))
    {
        return ucr -> data;
    }
    return NULL;
}

//静态查找2
static struct Node_wgc* find2(List* head, void* key1, void* key2, int (*cmp)(void* key1, void* key2, void*))
{
    struct Node_wgc* ucr;
    for(ucr = head -> tou.next; ucr != &(head -> tou); ucr = ucr -> next)
    {
        if(cmp(key1, key2, ucr -> data) == 0)
        {
            break;
        }
    }
        return ucr;
}

//查找数据2
void* find_wgc2(List* head, void* key1, void* key2, int (*cmp)(void* key1, void* key2, void*))
{
    struct Node_wgc* ucr;
    ucr = find2(head, key1, key2, cmp);
    if(ucr != &(head -> tou))
    {
        return ucr -> data;
    }
    return NULL;
}

//成功删除某个数据返回0，失败返回1。
int dale_wgc(List* head, void* key, int (*cmp)(void* key, void*))
{
	struct Node_wgc* ucr;
	ucr = find(head, key, cmp);
    	if(ucr == &(head -> tou))
    	{
        	return 1;
    	}
    	ucr -> next -> prev = ucr -> prev;
	ucr -> prev -> next = ucr -> next;
	free(ucr);
	return 0;
}

//修改成功返回0，修改失败返回1。
int modif_wgc(List* head, void* key, void* new_key, int (*cmp)(void* key, void*))
{
	struct Node_wgc* ucr;
	ucr = find(head, key, cmp);
	if(ucr == &(head -> tou))
	{
		return 1;
	}
	memcpy(ucr -> data, new_key, head -> size);
	return 0;
}

/*int sort(List* head)
{
        struct Node_wgc* ucr, *q;
        for(ucr = head -> tou.next; ucr != head -> tou.prev; ucr = ucr -> next)
        {
                if((ucr -> data -> xs_math) > (ucr -> next -> data -> xs_math))
                {
                        memcpy(q, ucr -> data, head -> size);
                        memcpy(ucr -> data, ucr -> next -> data, head -> size);
                        memcpy(ucr -> next -> data, ucr -> data, head -> size);
                }
        }
}*/

//销毁链表
void destory(List* head)
{
    struct Node_wgc* ucr, *save = NULL;
    for(ucr = head -> tou.next; ucr != &head -> tou; ucr = save)
    {
        save = ucr -> next;
        free(ucr);
    }
    free(head);
}

                         
